package cn.wolfcode.service.Impl;

import cn.wolfcode.exception.BaseException;
import cn.wolfcode.mapper.UsersMapper;
import cn.wolfcode.pojo.Users;
import cn.wolfcode.service.UsersService;
import cn.wolfcode.dto.UserRegisterDTO;
import cn.wolfcode.utils.GeneralUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    UsersMapper usersMapper;
    @Autowired
    GeneralUtils  generalUtils;

    public Users userLogin(Users users) {
        return usersMapper.getUser(users.getPhone());
    }

    public void userregister(UserRegisterDTO users) throws BaseException {
        if (usersMapper.getUser(users.getUsers().getPhone())!=null) {
            throw new BaseException("用户已注册,请勿重复注册");
        }
        if (!users.getUsers().getPassword().equals(users.getRpassword())) {
            throw new BaseException("两次密码不一致");
        }
        String password = DigestUtils.md5DigestAsHex(users.getUsers().getPassword().getBytes());
        users.getUsers().setPassword(password);
        usersMapper.userregister(users.getUsers());
    }

    public Users userinfo(HttpServletRequest request) throws Exception {
        String phone = generalUtils.getPhone(request);
        return usersMapper.getUser(phone);
    }
}
